package com.nit.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nit.server.common.QueryPageParam;
import com.nit.server.common.Result;
import com.nit.server.common.UseApplyParam;
import com.nit.server.entity.*;
import com.nit.server.service.CommunityService;
import com.nit.server.service.UseapplyService;
import com.nit.server.service.UsegoodsService;
import com.nit.server.service.UserecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *

 */
@RestController
@RequestMapping("/useapply")
public class UseapplyController {
    @Autowired
    private UseapplyService useapplyService;
    @Autowired
    private UsegoodsService usegoodsService;
    @Autowired
    private UserecordService userecordService;
    //新增
    @PostMapping("/add")
    public Result add(@RequestBody Useapply useapply){
        return useapplyService.save(useapply)?Result.success():Result.fail();
    }
    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam String useapplyId){
        return useapplyService.removeById(useapplyId)?Result.success():Result.fail();
    }
    @PostMapping("/listPage")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result listPage(@RequestBody QueryPageParam pageParam){
        Page<Useapply> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());
        String cId = (String)pageParam.getParams().get("cId");
        String userId = (String)pageParam.getParams().get("userId");
        QueryWrapper<Useapply> queryWrapper = new QueryWrapper();
        queryWrapper.apply("a.user_id = c.user_id and a.usegoods_id = b.usegoods_id ");
        if (StringUtils.isNotBlank(cId)){
            queryWrapper.eq("b.c_id",cId);
        }
        if (StringUtils.isNotBlank(userId)){
            queryWrapper.eq("a.user_id",userId);
        }
        IPage result = useapplyService.pageCC(page,queryWrapper);

        return Result.success(result.getTotal(), result.getRecords());
    }
    //通过
    @PostMapping("/checkyes")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result checkyes(@RequestBody UseApplyParam param){
        System.out.println(param);
        int userID = param.getUserId();
        int useapplyCount = param.getUseapplyCount();
        int adminId = param.getAdminId();
        String useapplyId = String.valueOf(param.getUseapplyId());
        String usegoodsId = String.valueOf(param.getUsegoodsId());
        Usegoods usegoods = usegoodsService.getById(usegoodsId);
        int newnum = usegoods.getUsegoodsCount()-useapplyCount;
        usegoods.setUsegoodsCount(newnum);
        usegoodsService.updateById(usegoods);

        Userecord userecord = new Userecord();
        userecord.setUsegoodsId(Integer.valueOf(usegoodsId));
        userecord.setUseapplyCount(useapplyCount);
        userecord.setUserId(userID);
        userecord.setAdminId(adminId);
        userecord.setUserecordStatus(1);
        userecordService.save(userecord);
        return useapplyService.removeById(useapplyId)?Result.success():Result.fail();
    }
    //未通过
    @PostMapping("/checkno")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result checkno(@RequestBody UseApplyParam param){
        int userID = param.getUserId();
        int useapplyCount = param.getUseapplyCount();
        String useapplyId = String.valueOf(param.getUseapplyId());
        String usegoodsId = String.valueOf(param.getUsegoodsId());
        int adminId = param.getAdminId();
        Userecord userecord = new Userecord();
        userecord.setUsegoodsId(Integer.valueOf(usegoodsId));
        userecord.setUseapplyCount(useapplyCount);
        userecord.setUserId(userID);
        userecord.setAdminId(adminId);
        userecord.setUserecordStatus(2);
        userecordService.save(userecord);
        return useapplyService.removeById(useapplyId)?Result.success():Result.fail();
    }
}
